<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="班次号" prop="classesNo">
            <el-input
              v-model="queryParams.classesNo"
              placeholder="请输入班次号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班结人" prop="classesMan">
            <el-input
              v-model="queryParams.classesMan"
              placeholder="请输入班结人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班结时间" prop="endDate">
            <el-date-picker
              style="width: 190px"
              v-model="dateRangeCreatedDate"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="global_el_rowStyle">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
    </el-row>

    <el-table :data="summaryList" :fit="true" stripe>
      <af-table-column label="班结主键id" align="left" prop="id" v-if="false" />
      <af-table-column label="班次号" align="left" prop="classesNo" />
      <af-table-column label="班次开始时间" align="left" prop="startDate">
      </af-table-column>
      <af-table-column label="班次结束时间" align="left" prop="endDate">
      </af-table-column>
      <af-table-column
        label="油站id"
        align="left"
        prop="stationId"
        v-if="false"
      />
      <af-table-column label="油站名称" align="left" prop="stationName" />
      <af-table-column label="订单数" align="left" prop="orderNum" />
      <af-table-column label="小票数量" align="left" prop="printNum" />
      <af-table-column label="数量(L/kg)" align="left" prop="saleLiters" />
      <af-table-column label="应收金额（元）" align="left" prop="saleAmt" />
      <af-table-column label="实收金额（元）" align="left" prop="amt" />
      <af-table-column label="优惠金额（元）" align="left" prop="discountAmt" />
      <af-table-column label="微信支付金额（元）" align="left" prop="wxAmt" />
      <af-table-column label="退款金额（元）" align="left" prop="refundAmt" />
      <af-table-column
        label="支付宝支付金额（元）"
        align="left"
        prop="zfbAmt"
      />
      <af-table-column
        label="电子卡支付金额（元）"
        align="left"
        prop="dzkAmt"
        v-if="false"
      />
      <af-table-column label="班结人" align="left" prop="classesMan" />
      <af-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="120px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handlelook(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            >下载</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="printClassInfo(scope.row)"
            v-if="reprint"
            >补打小票</el-button
          >
        </template>
      </af-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看班结数据 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body class="uncon">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="true"
        label-width="68px"
      >
        <el-form-item prop="carNumber">
          开班时间：{{ opo.startDate }} 班结时间：{{ opo.endDate }}班结人：{{
            opo.classesMan
          }}
        </el-form-item>
      </el-form>
      <!--按员工汇总-->
      <span style="color: #ff9955; font-size: 15px">|</span
      ><span style="font-size: 15px">按员工汇总</span>
      <el-table show-summary :data="structureList" :summary-method="getTotal">
        <af-table-column label="员工姓名" align="left" prop="oilPersonnel" />
        <af-table-column label="负责枪号" align="left" prop="oilGun" />
        <af-table-column label="销量" align="left" prop="orderLiters" />
        <af-table-column label="销额" align="left" prop="amt" />
        <af-table-column label="销售笔数" align="left" prop="orderNo" />
      </el-table>
      <!--按油品汇总-->
      <span style="color: #ff9955; font-size: 15px">|</span
      ><span style="font-size: 15px">按油品汇总</span>
      <el-table :data="structureList1" show-summary :summary-method="getTotal2">
        <af-table-column label="油品" align="left" prop="oilName" />
        <af-table-column label="负责枪号" align="left" prop="oilGun" />
        <af-table-column label="销量" align="left" prop="orderLiters" />
        <af-table-column label="销额" align="left" prop="amt" />
        <af-table-column label="销售笔数" align="left" prop="orderNo" />
      </el-table>
      <!--按员工/支付方式汇总：    -->
      <span style="color: #ff9955; font-size: 15px">|</span
      ><span style="font-size: 15px">按员工/支付方式汇总</span>
      <el-table
        :data="structureList3"
        show-summary
        :summary-method="getTotal3"
        max-height="h-180"
      >
        <af-table-column label="员工姓名" align="left" prop="oilPersonnel" />
        <af-table-column label="负责枪号" align="left" prop="oilGun" />
        <af-table-column label="微信笔数" align="left" prop="wxNum" />
        <af-table-column label="支付宝笔数" align="left" prop="zfbNum" />
        <af-table-column label="现金笔数" align="left" prop="xjNum" />
        <af-table-column label="优惠" align="left" prop="discountAmt" />
        <af-table-column label="优惠劵" align="left" prop="discountCouponAmt" />
        <af-table-column label="微信金额" align="left" prop="wxAmt" />
        <af-table-column label="支付宝金额" align="left" prop="zfbAmt" />
        <af-table-column label="现金金额" align="left" prop="xjAmt" />
        <af-table-column label="会员卡支付" align="left" prop="dzkAmt" />
        <af-table-column label="退款金额" align="left" prop="refundAmt" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listSummary } from "@/api/station/summary";
import { exportStructure, printClassInfo } from "@/api/station/structure";
import {
  listPersonnelPayStructure,
  listClass,
  listPersonnelStructure,
  listOilStructure,
} from "@/api/station/structure";
import { listManage } from "@/api/station/manage";

export default {
  name: "Overview_AccountingReport",
  data() {
    return {
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      summaryList: [],
      dateRangeCreatedDate: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classesNo: "",
        startDate: null,
        endDate: null,
      },
      queryParamPayOrder: {
        pageNum: 1,
        pageSize: 10,
        classesNo: "",
        startDate: null,
        endDate: null,
      },

      // 查询参数
      queryParam: {
        orderNo: null,
        stationId: null,
        stationName: null,
        oilGun: null,
        oilName: null,
        oilPirce: null,
        oilType: null,
        consumerId: null,
        consumer: null,
        amt: null,
        orderLiters: null,
        payType: null,
        payWay: null,
        payDate: null,
        oilPersonnel: null,
        orderType: null,
        printNum: null,
        classStructureNo: null,
        classStartDate: null,
        classStructureDate: null,
        classStructureMan: null,
        oils: null,
        receivableAmt: null,
        receivedAmt: null,
        discountAmt: null,
        discountCouponAmt: null,
        discountCoupon: null,
        wxAmt: null,
        zfbAmt: null,
        posAmt: null,
        xjAmt: null,
        didiAppAmt: null,
        tyAppAmt: null,
        otherAmt: null,
        dzkAmt: null,
        score: null,
        memberNo: null,
        dzkAmt: null,
        carNo: null,
        customerPhone: null,
        customerGrade: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      structureList: [],
      structureList1: [],
      structureList3: [],
      opo: {
        classStartDate: null,
        classStructureMan: null,
        classStructureDate: null,
        classStructureNo: null,
      },
      reprint: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      listManage({
        pageNum: 1,
        pageSize: 1,
      }).then((response) => {
        const equipmentList = response.rows;
        if (!!equipmentList) {
          if ((equipmentList[0] || {}).deviceType == "1") {
            this.reprint = true;
          }
        }
      });

      this.queryParams.levelId = this.levelId;

      listSummary(
        this.addDateRange(this.queryParams, this.dateRangeCreatedDate)
      ).then((response) => {
        this.summaryList = response.rows;
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classesNo: null,
        startDate: null,
        endDate: null,
        stationId: null,
        stationName: null,
        orderNum: null,
        printNum: null,
        saleLiters: null,
        saleAmt: null,
        wxAmt: null,
        zfbAmt: null,
        dzkAmt: null,
        classesMan: null,
        levelId: this.levelId,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRangeCreatedDate = [];
      this.handleQuery();
    },
    handlelook(row) {
      //this.$router.push({path:'/station/structure',query:{classesNo:row.classesNo,stationId:row.stationId,name:row.classesMan}});
      this.open = true;
      this.title = "查看班结明细";
      this.queryParamPayOrder.classesMan = row.classesMan;
      this.queryParamPayOrder.classesNo = row.classesNo;
      this.queryParamPayOrder.stationId = row.stationId;
      this.queryParamPayOrder.startDate = row.startDate;
      this.queryParamPayOrder.endDate = row.endDate;
      listClass(this.queryParamPayOrder).then((response) => {
        this.opo = response.data;
        this.getStructureList();
        this.getStructureList2();
        this.getStructureList3();
      });
    },
    //打印班结小票
    printClassInfo(row) {
      printClassInfo({
        stationId: row.stationId,
        classStructureNo: row.classesNo,
      }).then((response) => {
        this.msgSuccess("班结打印成功");
      });
    },
    /** 查询班结管理列表 */
    getStructureList() {
      listPersonnelStructure(this.queryParamPayOrder).then((response) => {
        this.structureList = response.rows;
      });
    },
    getStructureList2() {
      listOilStructure(this.queryParamPayOrder).then((response) => {
        this.structureList1 = response.rows;
      });
    },
    getStructureList3() {
      listPersonnelPayStructure(this.queryParamPayOrder).then((response) => {
        this.structureList3 = response.rows;
      });
    },
    getTotal(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "orderLiters") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "amt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "orderNo") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(0);
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
    getTotal2(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "orderLiters") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "amt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "orderNo") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(0);
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
    getTotal3(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "wxNum") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(0);
        } else if (column.property === "zfbNum") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(0);
        } else if (column.property === "xjNum") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(0);
        } else if (column.property === "discountAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "discountCouponAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "wxAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "zfbAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "xjAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "dzkAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else if (column.property === "refundAmt") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else {
          sums[index] = "";
        }
      });
      return sums;
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const query = {
        stationId: row.stationId,
        classStructureNo: row.classesNo,
      };
      this.$confirm("是否确认导出此条记录?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportStructure(query);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss">
.uncon {
  .el-table {
    overflow: auto;
  }
  .el-table--scrollable-x .el-table__body-wrapper {
    overflow-x: visible;
  }
  .el-table__body-wrapper {
    overflow: visible;
  }
  .el-table__header-wrapper {
    overflow: visible;
  }
  .el-table__footer-wrapper {
    overflow: visible;
  }
  .el-table::after {
    position: relative !important;
  }
}
</style>
